package com.weiaibenpao.demo.chislim.adater;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.MyGameBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wlx on 2017/10/12.
 */

public class MyGameAdapter extends RecyclerView.Adapter<MyGameAdapter.ViewHolder> {

    private final LayoutInflater inflater;

    private List<MyGameBean.DataBean.ListBean> list;
    private MyGameAdapter.OnItemClickListener listener;
    public MyGameAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void setOnItemClick(MyGameAdapter.OnItemClickListener listener){
        this.listener=listener;
    }

    public void addDate(List<MyGameBean.DataBean.ListBean> list){
        this.list.addAll(list);
        Log.e("list", "addDate: "+list.size() );
        notifyDataSetChanged();
    }
    public void refresh(List<MyGameBean.DataBean.ListBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public void clear(){
        list.clear();


    }
    public interface OnItemClickListener {
        void onItemClick(MyGameBean.DataBean.ListBean listBean);

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_unofficial_match, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final MyGameBean.DataBean.ListBean bean = list.get(position);
        holder.img_check.setVisibility(View.GONE);
        holder.tvMatchId.setText(bean.getActId());
        holder.textMarathonStartTime.setText("开始时间："+ bean.getSportsStartTime());
        holder.tvTimeLimit.setText("完赛时间:"+bean.getThroughTime());
        holder.textMarathonTitle.setText(bean.getMarathonTitle());
        holder.textNumber.setText("报名人数："+bean.getJoinNumber()+"/"+bean.getNumber()+"人");
        if (bean.getStatus()==1){
            holder.img_check.setVisibility(View.VISIBLE);
            holder.statusBtn.setVisibility(View.GONE);
        }else {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(bean);
                }
            });
           if (bean.getDmessage().equals("已完赛")||bean.getDmessage().equals("未报满")||bean.getDmessage().equals("待审核")||bean.getDmessage().equals("已结束")){
               holder.statusBtn.setBackgroundResource(R.drawable.default_select);
               holder.statusBtn.setTextColor(Color.parseColor("#000000"));
           }else {
               holder.statusBtn.setTextColor(Color.parseColor("#ffffff"));
               if (bean.getDmessage().equals("进行中")){
                   holder.statusBtn.setChecked(false);
               }if (bean.getDmessage().equals("备赛中")){
                   holder.statusBtn.setChecked(true);
               }
           }
            holder.statusBtn.setText(bean.getDmessage());
        }
        Log.e("wlx33", "onBindViewHolder: "+bean.getResult() );
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_match_name)
        TextView textMarathonTitle;
        @BindView(R.id.tv_start_time)
        TextView textMarathonStartTime;
        @BindView(R.id.tv_enrollment)
        TextView textNumber;
        @BindView(R.id.tv_time_limit)
        TextView tvTimeLimit;
        @BindView(R.id.status_btn)
        CheckBox statusBtn;
        @BindView(R.id.item_img_check)
        ImageView img_check;
        @BindView(R.id.tv_match_id)
        TextView tvMatchId;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            statusBtn.setVisibility(View.VISIBLE);
            img_check.setVisibility(View.VISIBLE);
            statusBtn.setClickable(false);
        }
    }
}
